<template>
    <div class="page">
      <div class="left">
        <img src="../assets/image/login_background.png" alt="" />

        <div class="left-content">
          <div class="left-content-header">
            <img class="logo" :src="logo" />
            <p class="system-title">{{ name }}</p>
            <p class="system-tips">Vue2 + Element UI</p>
          </div>
          <div class="copyright">Version {{ version }} · {{ author }}</div>
        </div>
      </div>
      <div class="right">
        <p class="weclomTitle">欢迎回来</p>
        <p class="weclomTips">登录 {{ name }} 后台管理系统</p>

        <div class="login-from-box" ref="loginFormBox">
          <!-- 手机号+验证码登录 -->
          <template v-if="loginType == 1">
            <div class="MobileAndPasswordLogin"></div>
          </template>

          <!-- 用户名+密码登录 -->
          <template v-if="loginType == 2">
            <div class="UsernameAndPasswordLogin">
              <div class="loginfrom">
                <el-form ref="form" :model="form">
                  <el-form-item>
                    <el-input
                      v-model="form.username"
                      name="username"
                      placeholder="请输入用户名"
                      :disabled="GetLoginLoadingStatus"
                      autocomplete="on"
                      autofocus
                    >
                      <i slot="prefix" class="el-input__icon el-icon-user"></i
                    ></el-input>
                  </el-form-item>
                  <el-form-item style="margin-bottom: 5px">
                    <el-input
                      name="password"
                      v-model="form.password"
                      placeholder="请输入登录密码"
                      show-password
                      :disabled="GetLoginLoadingStatus"
                      autocomplete="on"
                    >
                      <i slot="prefix" class="el-input__icon el-icon-lock"></i>
                    </el-input>
                  </el-form-item>

                  <el-form-item style="margin-bottom: 5px">
                    <el-row>
                      <el-col :span="6">
                        <el-checkbox
                          v-model="RememberPassword"
                          :disabled="GetLoginLoadingStatus"
                          >记住密码</el-checkbox
                        ></el-col
                      >

                      <el-col :offset="13" :span="5"
                        ><span>找回密码</span></el-col
                      >
                    </el-row>
                  </el-form-item>

                  <el-form-item style="margin-bottom: 15px">
                    <el-button
                      size="100"
                      type="primary"
                      :loading="GetLoginLoadingStatus"
                      @click="clickLogin"
                      >登录</el-button
                    >
                  </el-form-item>
                </el-form>
              </div>
            </div>
          </template>
        </div>
        <!-- 第三方登录方式 -->
        <ul class="third-list">
          <li class="third-list-li" title="微信登录">
            <i class="overall-icon third-list-li-icon icon-weixin"></i>
          </li>
          <li class="third-list-li" title="QQ登录">
            <i class="overall-icon third-list-li-icon icon-QQ"></i>
          </li>
          <li class="third-list-li" title="手机验证码登录">
            <i class="overall-icon third-list-li-icon icon-shouji1"></i>
          </li>
          <li class="third-list-li" title="邮箱验证码登录">
            <i class="overall-icon third-list-li-icon icon-email"></i>
          </li>
        </ul>
      </div>
    </div>
</template>

<script>

import { mapMutations, mapGetters } from "vuex";
import API from "@/api";
import SystemConfig from "@/config";
const { systemInfo } = SystemConfig;
const SystemStoreModuleName = systemInfo.StoreModuleName;

export default {
  inject: ["systemInfo"],
  watch: {
    loginType(newVal) {
      console.log(newVal);
      let boxDom = this.$refs["loginFormBox"];
      boxDom.animate(
        {
          transform: "rotateY(90deg)",
        },
        {
          duration: 300, //  动画时长  (单位毫秒)
          easing: "linear", // 平滑
          iterations: 1, //  重复次数  （无限循环：Infinity）
        }
      );
    },
  },

  computed: {
    ...mapGetters(SystemStoreModuleName, [
      "MenusListFormatVueRouter",
      "GetLoginLoadingStatus",
      "GetRememberPassword",
    ]),
  },
  data() {
    return {
      // 登录方式
      loginType: 2,
      // 登录表单
      form: {
        type: null,
        email: null,
        username: "",
        password: "",
      },
      // 是否记住密码
      RememberPassword: false,
    };
  },

  methods: {
    clickThird(type) {
      let loginType = this.loginType;

      switch (type) {
        case "wechat":
          console.log("微信登录");
          break;
        case "qq":
          console.log("qq登录");
          break;
        case "mobile":
          console.log("手机验证码登录");
          if (loginType == 2) {
            this.loginType = 1;
          } else if (loginType == 1) {
            this.loginType = 2;
          }
          break;
        case "email":
          console.log("邮箱验证码登录");
          break;
      }
    },
    ...mapMutations(SystemStoreModuleName, [
      "SET_LOGIN_USER_INFO",
      "SetRouterOriginList",
      "SET_LOGIN_LOADING_STATUS",
      "SetRouterOriginList",
      "SET_LOGIN_TOKEN",
    ]),
    // 点击登录
    clickLogin() {
      // 判断是否开启后端服务
      const { serverStatus } = this.systemInfo;
      if (!serverStatus) {
        alert("系统服务已关闭");
        return;
      }
      // let password = encrypt(this.form.password);
      let password = this.form.password;
      let username = this.form.username;
      if (this.GetLoginLoadingStatus) {
        return;
      } else if (!username) {
        this.$message.error("请输入用户名");
        return;
      } else if (!password) {
        this.$message.error("请输入登录密码");
        return;
      }
      // 设置登录等待状态 = true
      this.SET_LOGIN_LOADING_STATUS(true);
      // 开始执行登录流程
      API.user
        .login({ type: 1, username, password })
        .then((res) => {
          let { data } = res;

          let { userInfo, token } = data;
          // 保存用户信息
          this.SET_LOGIN_USER_INFO(userInfo);
          // 存储到Vuex
          this.SET_LOGIN_TOKEN({
            token,
          });
          return API.Manage.MenuList();
        })
        .then((MenusListResult) => {
          let { data } = MenusListResult;
          // 取出所有菜单权限
          let { list } = data;
          // 如果没有任何菜单权限
          if (!list.length) {
            throw { code: 500, msg: "菜单数据异常，未拥有任何菜单权限" };
          }
          // 新版本 设置原数据数组
          this.SetRouterOriginList(list);
          // 动态新增路由菜单
          this.MenusListFormatVueRouter.forEach((item) => {
            this.$router.addRoute("layout", item);
          });
          this.$router.replace("/");
        })
        .then(() => {
          // 设置登录等待状态 = false
          this.SET_LOGIN_LOADING_STATUS(false);
          // 弹出登录成功提示
          this.$message({
            message: "登录成功",
            type: "success",
          });
        })
        .catch((err) => {
          console.log(err);
          // 设置登录等待状态 = false
          this.SET_LOGIN_LOADING_STATUS(false);
          // 弹出错误提示
          this.$message.error(err.msg);
        });
    },
  },
  created() {
    this.logo = this.systemInfo.logo;
    this.name = this.systemInfo.name;
    this.version = this.systemInfo.version;
    this.author = this.systemInfo.author;
  },
};
</script>

<style scoped="scoped" lang="scss">
.page {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-wrap: nowrap;
}

.left {
  width: 30%;
  height: 100%;
  position: relative;
}

.left::after {
  content: "";
  position: absolute;
  background: #0009;
  left: 0;
  top: 0;
  z-index: 10;
  width: 100%;
  height: 100%;
}

.left-content {
  position: absolute;
  height: 100%;
  width: 100%;
  left: 0;
  top: 0;
  z-index: 11;
  display: flex;
  box-sizing: border-box;
  align-items: center;
  padding: 18px 0px;
  flex-direction: column;
  justify-content: space-between;
}

.left-content .logo {
  width: 110px;
  height: 110px;
}

.left-content .system-tips,
.left-content .system-title {
  color: white;
  font-weight: bold;
}

.left-content .system-title {
  font-size: 28px;
}

.left-content .system-tips {
  margin-top: 20px;
  font-size: 15px;
}

.left-content .copyright {
  font-size: 15px;
  color: white;
}

.left-content-header {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.right {
  flex: 1;
  height: 100vh;
  display: flex;
  background: white;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

img {
  width: 100%;
  height: 100%;
}

.from {
  flex: 1;
  height: 100vh;
}

.weclomTitle {
  text-align: center;
  font-size: 23px;
  margin-bottom: 15px;
  width: 80%;
  user-select: none;
}

.weclomTips {
  font-size: 18px;
  text-align: center;
  color: gray;
  user-select: none;
}

.loginfrom {
  width: 280px;
  margin-top: 25px;
}

.loginfrom ::v-deep .el-button {
  width: 100%;
}

.third {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;

  &-list {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 5px;
    gap: 25px;

    &-li {
      width: 34px;
      height: 34px;
      border-radius: 50%;
      background: rgb(233, 228, 228);
      text-align: center;
      line-height: 34px;

      &:last-child {
        margin-right: 0px;
      }

      &-icon {
        font-size: 20px;
      }

      &-wechat {
        font-size: 18px;
      }
    }
  }
}

.login-box {
  width: 440px;
  background: #000;
  border-radius: 16px;
  background: #fff;
  flex-direction: column;
}

/* Tag标签 */
.tag {
  margin-top: 15px;
  height: 50px;
  line-height: 50px;
  padding-left: 30px;
  position: relative;
  color: #c3c3c3;
  font-size: 18px;
  letter-spacing: 4px;
}

/* 标签焦点样式 */
.tag-slider {
  width: 43px;
  height: 4px;
  border-radius: 10px;
  background-color: #42b983;
  position: absolute;
  bottom: 0px;
  display: none;
  left: 0px;
  transition: all 0.3s linear;
}

.tag span {
  margin-right: 15px;
  text-align: center;
}

/* Tag 选中样式 */
.tag-tips-checked {
  color: #000000;
}

.login {
  margin: 0 auto;
  width: 300px;
  height: 190px;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}

.register {
  margin: 0 auto;
  width: 300px;
  flex-direction: column;
  height: 180px;
}

.login,
.register {
  margin-top: 15px;
}

.input {
  width: 100%;
  display: block;
  outline: none;
  border-radius: 40px;
  text-indent: 20px;
  border: none;
  background-color: #e3e3e366;
  font-size: 18px;
  height: 55px;
}

.login-from-box {
  padding: 10px;
}

.loginBtn {
  width: 60%;
  height: 50px;
  margin: 0 auto;
  border-radius: 60px;
  background-color: #42b983;
  color: #fff;
  line-height: 50px;
  letter-spacing: 4px;
  margin-top: 15px;
  cursor: default;
  text-align: center;
}

.loginBtn:hover {
  background-color: cornflowerblue;
}

.loginBtn:active {
  background-color: #42b983;
}

.agreement {
  text-align: center;
  margin: 15px 0px;
}

.grey,
.view-text {
  margin-left: 8px;
  font-size: 10px;
  vertical-align: middle;
}

.view-text {
  text-decoration: underline;
}

.checked-false {
  display: inline-block;
  width: 10px;
  height: 10px;
  vertical-align: middle;
  border-radius: 50%;
  background-color: #eee;
  border: 1px solid #c3c3c324;
}

.checked-true {
  background-color: #6495ed;
  border-color: #6495ed;
}

.grey {
  user-select: none;
  cursor: default;
  color: #2c3e50;
  margin-right: 8px;
}
</style>
